package 设计模式;

/**
 * 懒汉单例模式
 *
 * @Author: 李浩然
 * @Date: 2021/1/10 3:15 下午
 */
public class 单例模式 {
}

/**
 * 饿汉单例模式
 */
class Single1 {
    private static final Single1 s = new Single1();

    private Single1() {
    }

    public static Single1 getInstance() {
        return s;
    }
}

/**
 * 懒汉单例模式
 */
class Single2 {
    private static volatile Single2 s = null;

    private Single2() {
    }

    public static Single2 getInstance() {
        if (s == null) {
            synchronized (Single2.class) {
                if (s == null) {
                    s = new Single2();
                }
            }
        }
        return s;
    }
}

class Single3 {
    private static final Single3 s = new Single3();

    private Single3() {
    }

    ;

    public static Single3 getInstance() {
        return s;
    }
}

class Single4 {
    private static volatile Single4 s = null;

    private Single4() {
    }

    ;

    private static Single4 getInstance() {
        if (s == null) {
            synchronized (Single4.class) {
                if (s == null) {
                    s = new Single4();
                }
            }
        }
        return s;
    }
}
